package com.ruoyi.web.controller.self;


import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.self.Param.RmTestAnaswer;
import com.ruoyi.self.Param.RmTestinfoDTO;
import com.ruoyi.self.domain.RmTestinfo;
import com.ruoyi.self.domain.RmTestrecord;
import com.ruoyi.self.service.IRmTestinfoService;
import com.ruoyi.self.service.IRmTestrecordService;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 考题信息Controller
 * 
 * @author ruoyi
 * @date 2019-11-26
 */
@Controller
@RequestMapping("/self/testinfo")
public class RmTestinfoController extends BaseController
{
    private String prefix = "self/testinfo";

    @Autowired
    private IRmTestinfoService rmTestinfoService;

    @Autowired
    private IRmTestrecordService rmTestrecordService;

    @RequiresPermissions("self:testinfo:view")
    @GetMapping()
    public String testinfo()
    {
        return prefix + "/testinfo";
    }

    /**
     * 查询考题信息列表
     */
    @RequiresPermissions("self:testinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RmTestinfo rmTestinfo)
    {
        startPage();
        List<RmTestinfo> list = rmTestinfoService.selectRmTestinfoList(rmTestinfo);
        return getDataTable(list);
    }

    /**
     * 测评考题信息列表
     */
    @RequiresPermissions("self:testinfo:list")
    @PostMapping("/checktest")
    @ResponseBody
    public AjaxResult checktest(RmTestinfoDTO rmTestinfoDTO) throws JSONException {
        System.out.println(rmTestinfoDTO.getUseTime());
        String anaswer = rmTestinfoDTO.getAnaSwer();
        List<RmTestAnaswer> list= JSON.parseArray(anaswer,RmTestAnaswer.class);

       /* for ( RmTestAnaswer rmTestAnaswer:list) {
            System.out.println(rmTestAnaswer.getTestNumber() + " " + rmTestAnaswer.getValue());
        }*/

        int useTime= rmTestinfoDTO.getUseTime();
        String testId = rmTestinfoDTO.getTestId();
        System.out.println(testId);
        long testIdLong =Long.parseLong(testId);
        String testType =rmTestinfoDTO.getTestType();
        System.out.println(useTime);
        System.out.println(anaswer);
        Subject subject = SecurityUtils.getSubject();
        SysUser principal = (SysUser)subject.getPrincipal();
        int score = 0;
        Long id;
        String value;
        RmTestinfo rmTestinfo;
        String trueValue;

        for (RmTestAnaswer rmTestAnaswer: list) {
            System.out.println(rmTestAnaswer);
            id = Long.valueOf(rmTestAnaswer.getTestNumber());
            value = rmTestAnaswer.getValue();
            rmTestinfo = rmTestinfoService.selectRmTestinfoById(id);
            trueValue = rmTestinfo.getAnawers();
            if(trueValue.equals(value)) {
                score += 10;
                System.out.println("id is:" + id + "\n"  + "vaule is:" + value);
            }
        }

        System.out.println("user info is: " + principal.getDeptId() + "   " + principal.getLoginName());
        RmTestrecord rmTestrecord = new RmTestrecord();
        //rmTestrecord.setDeptId(principal.getDeptId());
        rmTestrecord.setId(testIdLong);
        rmTestrecord.setLoginName(principal.getLoginName());
        Date oprDate = new Date();
        System.out.println("System Date is : " + oprDate);
        rmTestrecord.setOprTime(oprDate);
        rmTestrecord.setUseTime(useTime);
        rmTestrecord.setScore(score);
        rmTestrecord.setTestType(testType);  //补考标识
        System.out.println(rmTestrecord.getOprTime());

        return toAjax(rmTestrecordService.updateRmTestrecord(rmTestrecord));
    }

    /**
     * 随机查询考题信息列表
     */
    /*@RequiresPermissions("self:testinfo:list")*/
    @PostMapping("/list2")
    @ResponseBody
    public List<RmTestinfo> list2(RmTestinfo rmTestinfo)
    {
        List<RmTestinfo> list = rmTestinfoService.selectRmTestinfoListRand(rmTestinfo);
        return list;
    }


    /**
     * 导出考题信息列表
     */
    @RequiresPermissions("self:testinfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RmTestinfo rmTestinfo)
    {
        List<RmTestinfo> list = rmTestinfoService.selectRmTestinfoList(rmTestinfo);
        ExcelUtil<RmTestinfo> util = new ExcelUtil<RmTestinfo>(RmTestinfo.class);
        return util.exportExcel(list, "testinfo");
    }

    @Log(title = "考题管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("self:testinfo:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RmTestinfo> util = new ExcelUtil<RmTestinfo>(RmTestinfo.class);
        List<RmTestinfo> testInfoList = util.importExcel(file.getInputStream());
        String message = rmTestinfoService.importTestinfo(testInfoList, updateSupport);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("self:testinfo:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RmTestinfo> util = new ExcelUtil<RmTestinfo>(RmTestinfo.class);
        return util.importTemplateExcel("考题数据");
    }


    /**
     * 新增考题信息
     */
    @GetMapping("/add/{tid}")
    public String add(@PathVariable("tid") String tid, ModelMap mmap)
    {
        mmap.put("tid", tid);
        return prefix + "/add";
    }

    /**
     * 新增保存考题信息
     */
    @RequiresPermissions("self:testinfo:add")
    @Log(title = "考题信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RmTestinfo rmTestinfo)
    {
        return toAjax(rmTestinfoService.insertRmTestinfo(rmTestinfo));
    }

    /**
     * 修改考题信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RmTestinfo rmTestinfo = rmTestinfoService.selectRmTestinfoById(id);
        mmap.put("rmTestinfo", rmTestinfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存考题信息
     */
    @RequiresPermissions("self:testinfo:edit")
    @Log(title = "考题信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RmTestinfo rmTestinfo)
    {
        return toAjax(rmTestinfoService.updateRmTestinfo(rmTestinfo));
    }

    /**
     * 删除考题信息
     */
    @RequiresPermissions("self:testinfo:remove")
    @Log(title = "考题信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rmTestinfoService.deleteRmTestinfoByIds(ids));
    }
}
